package jnpf.aop;

import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.constant.PermissionConstant;
import jnpf.permission.entity.PositionEntity;
import jnpf.permission.model.position.PositionCrForm;
import jnpf.permission.model.position.PositionUpForm;
import jnpf.permission.service.PositionService;
import jnpf.util.PermissionAspectUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/3/15 17:12
 */
@Slf4j
@Aspect
@Component
public class PositionPermissionAspect {

    @Autowired
    private UserProvider userProvider;
    @Autowired
    private PositionService positionService;

    /**
     * 分级管理切点
     */
    @Pointcut("@annotation(jnpf.annotation.PositionPermission)")
    public void pointcut() {
    }

    /**
     * 分级管理切点
     *
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 获取用户信息
        UserInfo userInfo = userProvider.get();
        // 跳过管理员
        if (PermissionAspectUtil.getIsAdministrator(userInfo)) {
            return pjp.proceed();
        } else {
            // 获取方法名
            String methodName = pjp.getSignature().getName();
            if (PermissionConstant.METHOD_CREATE.equals(methodName)) {
                PositionCrForm positionCrForm = (PositionCrForm) pjp.getArgs()[0];
                if (StringUtil.isNotEmpty(positionCrForm.getOrganizeId()) && PermissionAspectUtil.containPermission(positionCrForm.getOrganizeId(), userInfo, methodName)) {
                    return pjp.proceed();
                }
            } else if (PermissionConstant.METHOD_UPDATE.equals(methodName)) {
                // 获取id
                String id = String.valueOf(pjp.getArgs()[0]);
                PositionEntity entity = positionService.getInfo(id);
                PositionUpForm positionUpForm = (PositionUpForm) pjp.getArgs()[1];
                if (entity != null && StringUtil.isNotEmpty(entity.getOrganizeId()) && PermissionAspectUtil.containPermission(entity.getOrganizeId(), userInfo, methodName)) {
                    // 判断修改前后的parentId之后相同
                    if (positionUpForm.getOrganizeId().equals(entity.getOrganizeId())) {
                        return pjp.proceed();
                    }
                    if (PermissionAspectUtil.containPermission(positionUpForm.getOrganizeId(), userInfo, methodName)) {
                        return pjp.proceed();
                    }
                }
            } else if (PermissionConstant.METHOD_DELETE.equals(methodName)) {
                String id = String.valueOf(pjp.getArgs()[0]);
                PositionEntity entity = positionService.getInfo(id);
                if (StringUtil.isNotEmpty(entity.getOrganizeId()) && PermissionAspectUtil.containPermission(entity.getOrganizeId(), userInfo, methodName)) {
                    return pjp.proceed();
                }
            } else if (PermissionConstant.METHOD_SAVE.equals(methodName)) {
                String objectId = String.valueOf(pjp.getArgs()[0]);
                PositionEntity entity = positionService.getInfo(objectId);
                if (entity != null && StringUtil.isNotEmpty(entity.getOrganizeId()) && PermissionAspectUtil.containPermission(entity.getOrganizeId(), userInfo, PermissionConstant.METHOD_UPDATE)) {
                    return pjp.proceed();
                }
            }
            return ActionResult.fail("更新失败！你没有权限操作");
        }
    }

}
